#!/bin/bash

show_help() {
  echo -e "check floating/fixed ip"
  echo -e "Usage:"
  echo -e "$title [options] [repair|check|reboot|rebuild <fip>] [-- <openstack options...>]"
  echo -e "Options:"
  echo -e "\t--auth-url           keystone auth url"
  echo -e "\t--project            project name"
  echo -e "\t--projectid          project id"
  echo -e "\t--user | -u          user name"
  echo -e "\t--password | -p      password"
  echo -e "\t--help | -h          show this help and exit"
  echo -e "Parameters:"
  echo -e "\trepair <fip>         check floating/fixed ip and reboot failed instance"
  echo -e "\t                     \"ALL\" for all failed floating ips."
  echo -e "\trebuild <fip>        check floating/fixed ip and rebuild failed instance"
  echo -e "\t                     \"ALL\" for all failed floating ips."
  echo -e "\tcheck <fip>          check fip"
  echo -e "\treboot <fip>         reboot nova instance"
  echo -e "Examples:"
  echo -e "\t$title --project demo check ALL"
}

cmdopts=$(getopt --longoptions project:,projectid:,user:,password:,auth-url:,title:,fip:,help \
                     --options +hu:p: -- "$@")

# set positional parameters
eval set -- "$cmdopts"

title=do_fip
project=$OS_PROJECT_NAME
projectid=
username=$OS_USERNAME
password=$OS_PASSWORD
auth_url=$OS_AUTH_URL
fip=
op=

while true; do
  case "$1" in
    --title )
        title="$2"
        shift 2;;
    -h | --help )
        show_help
        exit ;;
    --project )
        project="$2"
        shift 2;;
    --projectid )
        projectid="$2"
        shift 2;;
    -u | --user )
        username="$2"
        shift 2;;
    -p | --password )
        password="$2"
        shift 2;;
    --auth-url )
        auth_url="$2"
        shift 2;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done

while true; do
  case "$1" in
    check | reboot | repair | rebuild)
        fip="$2"
        [ -n "$fip" ] || { echo "no ip given. try ALL ?" 1>&2; exit -1; }
        op="$1"
        shift 2;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done

#echo -e "os-project-domain-name:  $OS_PROJECT_DOMAIN_NAME"
#echo -e "os-user-domain-name:     $OS_USER_DOMAIN_NAME"
#echo -e "os-project-name:         $project"
#echo -e "os-username:             $username"
#echo -e "os-auth-url:             $auth_url"
#echo -e "os-interface:            $OS_INTERFACE"
#echo -e "os-identity-api-version: $OS_IDENTITY_API_VERSION"
#echo -e "os-region-name:          $OS_REGION_NAME"
#echo -e "os-auth-type:            $OS_AUTH_PLUGIN\n"
#
str1=
if [ -n "$op" ]; then
    str1="--$op $fip"
else
    show_help
    exit
fi

projectstr="--os-project-name $project"
if [ -n "$projectid" ]; then
    projectstr="--os-project-id $projectid"
fi

sudo ssh root@$controller docker exec -uroot kolla_toolbox \
 /bin/bash /nyyos/remote/check-servers-fip.sh \
 $str1 -- \
 --os-project-domain-name $OS_PROJECT_DOMAIN_NAME \
 --os-user-domain-name $OS_USER_DOMAIN_NAME \
 $projectstr \
 --os-username $username \
 --os-password $password \
 --os-auth-url $auth_url \
 --os-interface $OS_INTERFACE \
 --os-identity-api-version $OS_IDENTITY_API_VERSION \
 --os-region-name $OS_REGION_NAME \
 --os-auth-type $OS_AUTH_PLUGIN \
 $*

